home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / nan_news / toolkit / byteand.prg < prev    next >
Text File  |  1991-08-15  |  3KB  |  85 lines

  1. /*
  2.  * File......: BYTEAND.PRG
  3.  * Author....: Forest Belt, Computer Diagnostic Services, Inc.
  4.  * Date......: $Date:   15 Aug 1991 23:03:02  $
  5.  * Revision..: $Revision:   1.2  $
  6.  * Log file..: $Logfile:   E:/nanfor/src/byteand.prv  $
  7.  * 
  8.  * This is an original work by Forest Belt and is placed in the
  9.  * public domain.
  10.  *
  11.  * Modification history:
  12.  * ---------------------
  13.  *
  14.  * $Log:   E:/nanfor/src/byteand.prv  $
  15.  * 
  16.  *    Rev 1.2   15 Aug 1991 23:03:02   GLENN
  17.  * Forest Belt proofread/edited/cleaned up doc
  18.  * 
  19.  *    Rev 1.1   14 Jun 1991 19:51:12   GLENN
  20.  * Minor edit to file header
  21.  * 
  22.  *    Rev 1.0   01 Apr 1991 01:00:50   GLENN
  23.  * Nanforum Toolkit
  24.  *
  25.  */
  26.  
  27.  
  28.  
  29. /*  $DOC$
  30.  *  $FUNCNAME$
  31.  *     FT_BYTEAND()
  32.  *  $CATEGORY$
  33.  *     String
  34.  *  $ONELINER$
  35.  *     Perform bit-wise AND on two ASCII characters (bytes)
  36.  *  $SYNTAX$
  37.  *     FT_BYTEAND( <cByte1>, <cByte2> ) -> cByte
  38.  *  $ARGUMENTS$
  39.  *     <cByte1> and <cByte2> are characters from CHR(0) TO CHR(255).
  40.  *     May be passed in CHR() form, as character literals, or as expressions
  41.  *     evaluating to CHR() values.
  42.  *  $RETURNS$
  43.  *     Returns resulting byte, in CHR() form.  If parameters are faulty,
  44.  *     returns NIL.
  45.  *  $DESCRIPTION$
  46.  *     Can be used for any bit-wise masking operation.  In effect, this is a
  47.  *     bit-by-bit AND operation.  Equivalent to AND assembler instruction.
  48.  *
  49.  *     This function is presented to illustrate that bit-wise operations
  50.  *     are possible with Clipper code.  For greater speed, write .C or
  51.  *     .ASM versions and use the Clipper Extend system.
  52.  *  $EXAMPLES$
  53.  *     This code would mask out the high nibble (four most significant bits)
  54.  *     of the byte represented by chr(123) and leave the low nibble bits as in
  55.  *     the parameter byte.
  56.  *
  57.  *          cNewbyte := FT_BYTEAND( CHR(123), CHR(15) )
  58.  *          ? asc(cNewByte)  // result: 11
  59.  *          ? cNewByte       // result: non-printable character
  60.  *
  61.  *     For a demonstration of Clipper bit manipulations, compile and
  62.  *     link the program BITTEST.PRG in the Nanforum Toolkit source code.
  63.  *  $SEEALSO$
  64.  *     FT_BYTEOR() FT_BYTEXOR() FT_BYTENOT() FT_BYTENEG()
  65.  *  $END$
  66.  */
  67.  
  68. FUNCTION FT_BYTEAND(cByte1, cByte2)
  69.  
  70.   LOCAL nCounter, cNewByte
  71.  
  72.   IF valtype(cByte1) != "C" .or. valtype(cByte2) != "C" // parameter check
  73.      cNewByte := NIL
  74.   ELSE
  75.      cNewByte := chr(0)
  76.      for nCounter := 0 to 7           // test each bit position
  77.         if FT_ISBIT(cByte1, nCounter) .and. FT_ISBIT(cByte2, nCounter)
  78.            cNewByte := FT_BITSET(cNewByte, nCounter)
  79.         endif
  80.      next
  81.   ENDIF
  82.  
  83. RETURN cNewByte
  84.  
  85.